@echo off
echo ========================================
echo    Lottery Calculator - Simple Build Script v3.1
echo ========================================
echo Features: Network Fix + Basic Android Support
echo ========================================
echo.

setlocal enabledelayedexpansion

echo Step 1: Checking environment dependencies...
echo Checking Node.js...
node --version >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Node.js is not installed!
    pause
    exit /b 1
)
echo [SUCCESS] Node.js is installed






echo.
echo Step 3: Building React application...
call npm run build
if %errorlevel% neq 0 (
    echo [ERROR] React build failed!
    pause
    exit /b 1
)
echo [SUCCESS] React application build completed

echo.
echo Step 4: Running network fix script...
if exist "fix_android_network.ps1" (
    powershell -ExecutionPolicy Bypass -File fix_android_network.ps1
    echo [COMPLETED] Network configuration fixed
) else (
    echo [WARNING] Network fix script not found, skipping this step
)

echo.
echo Step 5: Adding Android target platforms...
echo Adding basic Android architecture support...
rustup target add aarch64-linux-android >nul 2>&1
echo [SUCCESS] Android target platform added

echo.
echo Step 6: Initializing Android project...
if not exist "src-tauri\gen\android" (
    echo Initializing Tauri Android...
    call npm run tauri android init
    if %errorlevel% neq 0 (
        echo [ERROR] Android initialization failed!
        pause
        exit /b 1
    )
    echo [SUCCESS] Android project initialization completed
) else (
    echo [SKIP] Android project exists, re-initializing...
    call npm run tauri android init
    if %errorlevel% neq 0 (
        echo [ERROR] Android re-initialization failed!
        pause
        exit /b 1
    )
    echo [SUCCESS] Android project re-initialization completed
)

echo.
echo Step 7: Basic Android configuration...
echo [SUCCESS] Using default Android configuration

echo.
echo Step 8: Building APK using Gradle...
cd src-tauri\gen\android

call gradlew-home.bat assembleRelease 
if %errorlevel% neq 0 (
    echo [ERROR] Gradle build failed!
    cd ..\..\..
    pause
    exit /b 1
)
cd ..\..\..
echo [SUCCESS] APK build completed

echo.
echo Step 9: Copying APK to root directory...
if exist "src-tauri\gen\android\app\build\outputs\apk\debug\app-debug.apk" (
    copy "src-tauri\gen\android\app\build\outputs\apk\debug\app-debug.apk" "lottery-calculator-final.apk" >nul
    echo [SUCCESS] APK copied to: lottery-calculator-final.apk
    
    for %%I in ("lottery-calculator-final.apk") do (
        echo APK size: %%~zI bytes
    )
) else (
    echo [ERROR] Generated APK file not found!
    pause
    exit /b 1
)

echo.
echo ========================================
echo           Build Completed!
echo ========================================
echo.
echo APK file: lottery-calculator-final.apk
echo Network issues fixed
echo Basic Android support enabled
echo.
echo Features:
echo - Network connection fix
echo - Standard Android compatibility
echo - Basic error handling
echo.
echo You can install this APK on Android devices for testing!
echo.
pause